<?php

/**
 * class for LoggedUser
 */
class LoggedUser {

	/**
	 *
	 * @var	User
	 */
	protected static $loggedUser = NULL;

	protected static $cookieName = 'fe_typo_user';

	public static function whoIsLogged($forceReload = false)
	{
		if (self::$loggedUser === NULL || $forceReload)
		{
			$uid = self::getUserIDFromSessions($_COOKIE[self::$cookieName]);
			
			// lebo typo nestihne dat session do db pri prvom prihlaseni, ked renderuje stranku
			if (!$uid) {
				$uid = $GLOBALS['TSFE']->fe_user->user['uid'];
			}
			
			if ($uid) {
				$userRepository = new UserRepository();
				$user = $userRepository->getOneByUid($uid);
				self::$loggedUser = $user;
			}
		}
		return self::$loggedUser;
	}
	
	protected static function getUserIDFromSessions($sesId) {
		$sesId = addslashes($sesId);
		$query = "SELECT ses_userid FROM fe_sessions WHERE ses_id = '$sesId' LIMIT 1";
		
		$user = DB::fetchFirst($query);
		
		if ($user) {
			return $user['ses_userid'];
		}
		return FALSE;
	}
}

?>